package ios.di.logviewer.test;

import ios.di.logviewer.util.DateUtil;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentFactory;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import org.dom4j.io.SAXWriter;

/**
 * 测试辅助类，用于生成日志文件，以供测试
 * @author LuoHouQi
 *
 */
public class CreateLog {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		createSampleLogFile();
	}
	
	
	public static void createSampleLogFile()
	{
		String path = "./sample.xml";
		File recourcesFile = new File(path);
		
		SAXReader reader = new SAXReader();
	    Document document =null;
	    
	    
			try {
				document = reader.read(recourcesFile);
				Element rootElement = (Element)document.getRootElement();
				
				int j=0;
				for(long i=0;i<200000;i++)
				{
					
					createElement(rootElement,i);
					if(i%100000==0)
					{
						System.out.println("Create Node "+i);
						document = writeDoc(document);
						rootElement = (Element)document.getRootElement();
					}
					
				}
				writeDoc(document);
				
			} catch (DocumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				
			}
			
			
			
			
	}
	public static Element createElement(Element root,long i)
	{
		Date date = new Date(new Date().getTime()+i*50);
		String dateStr = DateUtil.getDateUtil(DateUtil.PATTERN_1).convertToString(date);
		Element logEle = root.addElement("LOG");
		
		Element userEle = logEle.addElement("User");
		userEle.setText("Admin");
		
		Element errEle = logEle.addElement("ErrorCode");
		errEle.setText("0");
		
		Element opeEle = logEle.addElement("Operation");
		opeEle.setText("生成数据");
		
		Element detEle = logEle.addElement("Details");
		detEle.setText("耗时: 00时00分02秒");
		
		Element happenEle = logEle.addElement("Happen");
		happenEle.setText(dateStr);
		
		Element typeEle = logEle.addElement("type");
		typeEle.setText("仓储日销售");
		
		Element rqEle = logEle.addElement("rq");
		rqEle.setText(dateStr);
		
		Element gysEle = logEle.addElement("gys");
		gysEle.setText("15639");
		
		Element infoEle = logEle.addElement("info");
		infoEle.setText(Long.toString(i));
		
		return logEle;
		
		
	}
	private static Document writeDoc(Document doc) throws DocumentException {
		try {
			FileWriter writer = new FileWriter("./2009-3-31.XML");
			try {
				doc.write(writer);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				writer.close();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		SAXReader reader = new SAXReader();
		Document document = reader.read(new File("./2009-3-31.XML"));
		return document;
	}
}



